package com.alumni_circle.controller.home.addition;

import cn.hutool.core.util.ObjectUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.addition.ViewsArticleSum;
import com.alumni_circle.service.home.addition.impl.ViewsArticleSumServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 龙小虬
 * @since 2020-08-30 22:36
 */
@RestController
@RequestMapping("/viewsArticleSum")
public class ViewsArticleSumController extends BaseController<ViewsArticleSum> {

    @Resource
    ViewsArticleSumServiceImpl viewsArticleSumService;

    /**
     * 增加文章浏览
     *
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addViewsArticle", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "增加文章浏览", notes = "增加文章浏览", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="增加文章浏览",url="/viewsArticleSum/addViewsArticle")
    public ResponseModel addViewsArticle(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        QueryWrapper<ViewsArticleSum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", SecurityUtils.getId());
        queryWrapper.eq("article_id",baseId.getId());
        if(ObjectUtil.hasEmpty(viewsArticleSumService.getOne(queryWrapper))){
            ViewsArticleSum viewsArticleSum = new ViewsArticleSum();
            viewsArticleSum.setArticleId(baseId.getId());
            viewsArticleSum.setId(SecurityUtils.getId());
            viewsArticleSumService.save(viewsArticleSum);
            // 增加
            return new ResponseModel<>(true);
        }
        // 不增加
        return new ResponseModel<>(false);
    }

}
